<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_users_actions_block_FormGroupsAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		$formProvider = f_form_Provider::getInstance();
		
		$formProvider->setDocumentName("groups")
		             ->setModuleName("users")
		             ->setFormName("FormGroups");
		
		$values = $formProvider->getParams();
        
		$groups = m_users_documents_Group::createNew();
		
		if($formProvider->isSubmitted())
		{
			if($formProvider->isValid())
			{				
				$groups
					->setAuthor($this->getUser()->getlogin())
					->setLabel($values['formValues']['label'])
					->save();
				
				$docId = $this->getRequestParameter("docId");
				
				$node = $this->getDocumentProvider()->getByUniqueId($docId);
				
				$node->addGroup($groups)->save();
				$values['errors']['status'] = "success";
			}
			else
			{
				$values['errors'] = $formProvider->getErrors();
			}
		}

        if(!isset($values['errors']['status']))
        {
            $values['errors']['status'] = null;
        }

		$values["groups"] = $groups;
		$values["href"] = $this->getRequestParameter("href");
		$this->setModel($values);
		$this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo","users_Add"));
	}	
}	